package view;

import java.awt.Color;
import java.awt.Graphics;
import javax.swing.JPanel;
import view.viewPanels.ActGamesViewPanel;
import view.viewPanels.ArchGamesViewPanel;

/**
 * JPanel that displays the playing field
 */
@SuppressWarnings( "serial" )
public class BoardPanel extends JPanel {

    /**
     * the JPanel which contains this board panel
     */
    private JPanel container;

    /**
     * the size of a field
     */
    private int fieldsize;

    /**
     * the first color of the playing field
     */
    private Color color1 = new Color( 248, 248, 255 );

    /**
     * the second color of the playing field
     */
    private Color color2 = Color.lightGray;

    /**
     * constructor of the BoardPanel
     * 
     * @param container
     *            the JPanel which contains this board panel
     */
    public BoardPanel( JPanel container )
    {
	super();
	this.container = container;
    }

    /**
     * overridden paintComponent from JComponent
     * 
     * @param g
     *            the Graphics object to draw on
     */
    @Override
    public final void paintComponent( Graphics g )
    {
	super.paintComponent( g );

	this.fieldsize = getSize().width / 8;

	// draw game board background
	g.setColor( this.color2 );
	for ( int i = 0; i < 8; i++ )
	{
	    if ( g.getColor() == this.color1 )
	    {
		g.setColor( this.color2 );
	    }
	    else
	    {
		g.setColor( this.color1 );
	    }
	    for ( int j = 0; j < 8; j++ )
	    {
		g.fillRect( i * this.fieldsize, j * this.fieldsize, this.fieldsize, this.fieldsize );

		if ( g.getColor() == this.color1 )
		{
		    g.setColor( this.color2 );
		}
		else
		{
		    g.setColor( this.color1 );
		}
	    }
	}

	// draw figures
	if ( this.container == ChessFrame.actGamesViewPanel )
	{
	    ActGamesViewPanel actContainer = (ActGamesViewPanel)this.container;

	    if ( actContainer.getCtActGames().isSelected() )
	    {
		actContainer.getActGamesModel().getActualMatch().repaintFigures( this.fieldsize, g, -1 );
	    }

	}
	else if ( this.container == ChessFrame.archGamesViewPanel )
	{
	    ArchGamesViewPanel archContainer = (ArchGamesViewPanel)this.container;

	    if ( archContainer.getCtArchGames().isSelected() )
	    {
		archContainer.getArchGamesModel().getActualMatch().repaintFigures( this.fieldsize, g, -1 );
	    }
	}
    }

    /**
     * getter of fieldsize
     * 
     * @return the fieldsize
     */
    public final int getFieldsize()
    {
	return this.fieldsize;
    }

}
